package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingPlanPageDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.ProjectPlans;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Override
    public List<NursingPlanVO> selectAll() {
      return   nursingPlanMapper.selectAll();
    }

    @Override
    public PageResponse<NursingPlanVO> pageCondition(NursingPlanPageDto nursingPlanPageDto) {
        PageHelper.startPage(nursingPlanPageDto.getPageNum(),nursingPlanPageDto.getPageSize());
        Page<NursingPlanVO> pages = nursingPlanMapper.pageCondition(nursingPlanPageDto);
        PageResponse<NursingPlanVO> vos = PageResponse.of(pages, NursingPlanVO.class);
        return vos;
    }

    @Override
    public NursingPlanVO getById(Long id) {
        NursingPlanVO planVO = nursingPlanMapper.getById(id);
        planVO.setProjectPlans(nursingPlanMapper.getProjectsByPlanId(id));
        return planVO;
    }

    @Override
    @Transactional
    public void update(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        //进行计划表中的更新
        nursingPlanMapper.update(nursingPlan);
        //进行计划项目表中的更新
        //1、先删除原有的计划
        nursingPlanMapper.deleteProjectsByPlanId(nursingPlan.getId());
        //2、将planID插入到关联表中
        List<ProjectPlans> projectPlansList = nursingPlanDto.getProjectPlans().stream().peek(p -> p.setPlanId(nursingPlanDto.getId().toString())).collect(Collectors.toList());
        //3、重新批量插入对应的护理项数据
        nursingPlanMapper.insertProject(projectPlansList);
    }

    @Override
    public void delete(Long id) {
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setId(id);
        nursingPlan.setIsDelete(1);
        nursingPlanMapper.update(nursingPlan);
    }

    @Override
    public void changeStatus(Long id, Integer status) {
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setStatus(status.byteValue());
        nursingPlan.setId(id);
        nursingPlanMapper.update(nursingPlan);
    }

    @Override
    @Transactional
    public void insert(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        //在计划表中插入数据，插入之后回显ID
        nursingPlanMapper.insert(nursingPlan);
        //将护理项插入到计划项目关联表中
        //将planID插入到关联表中
        List<ProjectPlans> projectPlansList = nursingPlanDto.getProjectPlans().stream().peek(p -> p.setPlanId(nursingPlan.getId().toString())).collect(Collectors.toList());
        nursingPlanMapper.insertProject(projectPlansList);

    }

}
